//
//  DialogGuapai.swift
//  jiaoyi
//
//  Created by wjk on 2018/11/5.
//  Copyright © 2018 wjk. All rights reserved.
//

import UIKit
import SwiftyJSON

class DialogGuapai: UIView,UITextFieldDelegate {

    var cb:cancleBlock?
    var ob:okBlock?
    
    @IBOutlet weak var labelNo: UILabel!
    
    @IBOutlet weak var labelCate: UILabel!
    
    @IBOutlet weak var labelNum: UILabel!
    @IBOutlet weak var labelPirce: UILabel!
    @IBOutlet weak var tfNo: UITextField!
    
    @IBOutlet weak var labelDanwei: UILabel!
    @IBOutlet weak var labelAmount: UILabel!
    @IBOutlet weak var labelAllPrice: UILabel!
    @IBOutlet weak var labelLowNum: UILabel!
    @IBOutlet weak var labelShengyu: UILabel!
    
    var da:JSON!
    
    var fid:String!
    static public func showView(frame: CGRect, data:JSON, fid:String) -> DialogGuapai{
        let v = Bundle.main.loadNibNamed("DialogGuapai", owner: nil, options: nil)?.first as! DialogGuapai
        v.frame = frame
        
        v.fid = fid
        v.da = data
        v.tfNo.delegate = v
        v.labelDanwei.text = data["num_unit_text"].stringValue
        v.labelNo.text = data["alias"].stringValue
        v.labelCate.text = data["variety_name"].stringValue
        
        
        let formater = NumberFormatter()
        
        formater.positiveFormat = "#0.####"
//        formater.string(for:
        
        v.labelNum.text = "\(data["reset_num"].stringValue)/\(data["num"].stringValue) \(data["num_unit_text"].stringValue)"
        v.labelPirce.text = "\(data["price_yuan"].stringValue) \(data["price_unit_text"].stringValue)"
        v.labelLowNum.text = "\(data["min_num"].stringValue) \(data["num_unit_text"].stringValue)"
        v.labelShengyu.text = "\(data["reset_num"].stringValue) \(data["num_unit_text"].stringValue)"
//        let p = data["reset_num"].floatValue * data["price_yuan"].floatValue
//        v.labelAllPrice.text = "\(p)"
        return v
    }
    
    init(frame: CGRect, data:JSON) {
        super.init(frame: frame)
        //        return
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        //        fatalError("init(coder:) has not been implemented")
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        return true
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        if self.tfNo.text!.isEmpty == false {
            
            let p = jsonFormater(data: ["key":self.tfNo.text!])["key"].floatValue * self.da["price_yuan"].floatValue
            
            let formater = NumberFormatter()
            
            formater.positiveFormat = "#0.00"
            
            self.labelAllPrice.text = formater.string(from: NSNumber.init(value: p))//"\(p)"
            
            NetWorkRequest(.guapaideposit(fid: self.fid, tid: da["id"].stringValue, num: self.tfNo.text!)) { (res) -> (Void) in
                if res["code"] as! String == "000000" {
                    
//                    self.labelAmount.text = jsonFormater(data: res["data"] as Any)["deposit"].stringValue
                    
                    self.labelAmount.text = formater.string(from: jsonFormater(data: res["data"] as Any)["deposit"].numberValue)
                    
                }
            }
        }
        
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    @IBAction func ok(_ sender: UIButton) {
        if self.tfNo.text!.isEmpty {
            showMessage(message: "请输入数量")
            return
        }
        
//        let f = Float(self.tfNo.text!)
//
//        if  (f < self.da["min_num"].floatValue) {
//            showMessage(message: "请大于最低成交量")
//            return
//        }
//
//        if (f > self.da["reset_num"].floatValue) {
//            showMessage(message: "请大于最低成交量")
//            return
//        }
        
        ob?(["price":self.tfNo.text!])
    }    
    
    @IBAction func cancel(_ sender: UIButton) {
         cb?(["":""])
        self.removeFromSuperview()
    }
}
